var findEvnet = document.querySelector('.login-sign-event')
var findLogin = document.querySelector('.login-main')
var findSign = document.querySelector('.sign-up-main')

// 登陆
var addLogin =  function() {
  var loginbtn = document.querySelector('.loginBtn-login')
  loginbtn.addEventListener('click',  function(){
    findEvnet.classList.remove('show-login-sign-event')
    findLogin.classList.remove('show-login-main')
    console.log('loginBtn')
  })
}

// 注册
var addSign = function() {
  var singupbtn = document.querySelector('.singupBtn-sign')
  singupbtn.addEventListener('click', function(){
    // findEvnet.classList.remove('show-login-sign-event')
    // findSign.classList.remove('show-sign-up-main')
    // console.log('singupBtn')
    swal("未开放注册！")
  })
}

var _main = function() {
  addLogin()
  addSign()
}
// _main()

// 自动输入用户名和密码
var loginAdmin = document.querySelector('#login-username')
var loginPwd = document.querySelector('#login-userpwd')
var clickLoginBtn = function(loginUername, loginUserPwd) {
  loginAdmin.value = loginUername
  loginPwd.value = loginUserPwd
}
// clickLoginBtn("admin", "123456")


// 判断登陆信息
var judgeInput = function() {
 var userNameInput = loginAdmin.value
 var userPwdInput = loginPwd.value
 var UL = userNameInput.length
 var PL = userPwdInput.length
 console.log('username', typeof(UL))
 console.log('userpwd', typeof(PL))
 if( UL >= 2 && userNameInput == 'admin') {
   if (PL >= 6 && userPwdInput == '123456') {
   } else {
     swal('密码错误')
   }
 } else {
   swal('用户名错误')
 }
}


// 发送登陆信息
var sendLogin = function() {
  var Admin = 'admin'
  var userPwd = '123456'
  // console.log('login')
  var xhr = new XMLHttpRequest();
  self._xhr = xhr;
  xhr.open("POST", 'https://api.webrtc.win:6601/v1/customer/login');
  var data = JSON.stringify({
      user: Admin ,
      password: userPwd ,
  });
  xhr.onload = function () {
      if (this.status >= 200 && this.status < 300) {
          var res = JSON.parse(this.response);
          if (!!res.token){
              // console.log('token:' +res.token);
          }
      } else {
          alert('请求出错!');
      }
  };
  xhr.send(data);
}
sendLogin()




console.log('yiyiyiyi')
var needTologin = function() {
  var xhr = new XMLHttpRequest();
      self._xhr = xhr;
      xhr.open("POST", 'https://api.webrtc.win:6601/v1/customer/login');
      var data = JSON.stringify({
          user:'admin',
          password:'123456'
      });
      xhr.onload = function () {
          if (this.status >= 200 && this.status < 300) {

              var res = JSON.parse(this.response);
              if (!!res.token){
                  console.log('token:' +res.token);

                  var player = new PearPlayer('#pearvideo', {      //第一个参数为video标签的id或class
                      type: 'mp4',                //0播放视频的类型,目前只能是mp4
                      src: 'https://qq.webrtc.win/tv/pear.mp4',  //视频播放的src
                      token: res.token,           //与信令服务器连接的token,必须
                      algorithm: 'firstaid',      //0核心算法,默认firstaid
                      params: {                   //核心算法的参数
                          urgent: 2,                  //0urgent的块数,默认2
                          normal: 3,                  //0normal的块数,默认3
                          prefetch: 2                //p0refetch(用data channel下载)的块数,默认3
                      },
                      autoplay: true,            //是否自动播发视频,默认true
                      chunkSize: 512*1024,        //0每个chunk的大小,必须是32K的整数倍,默认1M
                      interval: 5000,             //0滑动窗口的时间间隔,单位毫秒,默认10s
                      auto: false,                //true为连续下载buffer,false则是只有当前播放时间与已缓冲时间小于slideInterval时下载buffer,如果是fmp4建议设为true,默认false
                      timeout: 5000,              //0连接data channel的超时时间,单位毫秒,默认5s
                      reconnectTimes: 0,          //超时重连次数,默认0次(未实现)
                      slideInterval: 10,          //0当前播放时间与已缓冲时间小于这个数值时触发窗口滑动,单位秒,默认20s
                      useDataChannel: true,       //是否开启data channel,默认true
                      useMonitor: true               //是否开启monitor,会稍微影响性能,默认true
                  });

                  player.on('exception', onException);
                  player.on('begin', onBegin);
                  player.on('progress', onProgress);
                  player.on('cloudspeed', onCloudSpeed);
                  player.on('fogspeed', onFogSpeed);
                  player.on('fograte', onWebRTCRate);
                  player.on('done', onDone);
  //
  //                player.setURL('https://qq.webrtc.win/tv/pear001.mp4');
              }
          } else {
              alert('请求出错!');
          }
      };
      xhr.send(data);

}

// needTologin()
